package com.company.structural.proxy_pattern.proxy;

import com.company.structural.proxy_pattern.real_subject.AccessValidator;
import com.company.structural.proxy_pattern.real_subject.Logger;
import com.company.structural.proxy_pattern.real_subject.RealSearcher;
import com.company.structural.proxy_pattern.subject.Searcher;

/**
 * 代理查询
 * @ClassName ProxySearcher
 * @Author tiezheng.han
 * @Date 2020/9/1 18:06
 **/
public class ProxySearcher implements Searcher {

    private RealSearcher searcher = new RealSearcher();
    private AccessValidator validator;
    private Logger logger;

    @Override
    public String doSearch(String userid, String keyword) {
        // 如果身份验证成功，则执行查询
        if(this.validator(userid)){
            String result = searcher.doSearch(userid,keyword);
            this.log(userid);
            return result;
        }
        return null;
    }

    /**
     * 创建访问验证对象并调用其validator()方法实现身份验证
     */
    public boolean validator(String userId){
        validator = new AccessValidator();
        return validator.validate(userId);
    }

    /**
     * 创建日志记录对象并调用其log()方法实现日志记录
     */
    public void log(String userId){
        logger = new Logger();
        logger.log(userId);
    }
}
